package fly.gdx.html.window;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.gwt.GwtApplication;
import com.badlogic.gdx.backends.gwt.GwtApplicationConfiguration;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

import fly.gdx.graphics.FlyRenderer2D;
import fly.shared.Event;

public class FlyWindow extends GwtApplication
{
	protected int width = 480;
	protected int height = 320;
	
	protected FlyWindowListener listener = null;
	
	
	@Override
	public ApplicationListener getApplicationListener()
	{
		return listener;
	}

	@Override
	public GwtApplicationConfiguration getConfig()
	{
		return new GwtApplicationConfiguration(this.width, this.height);
	}

	
	public FlyWindow()
	{
		this(480,320,null);
	}
	
	public FlyWindow(int w, int h)
	{
		this(w,h,null);
	}
	
	
	public FlyWindow(int w, int h, Event<Object> event_init)
	{
		this.width = w;
		this.height = h;
		this.listener = new FlyWindowListener(event_init);
	}
	
	
	public int GetWidth() {return this.getGraphics().getWidth();}
	public int GetHeight() {return this.getGraphics().getHeight();}
	
	
	public void SetEventRender(Event<FlyRenderer2D> render_event)
	{
		this.listener.event_render = render_event;
	}
}


class FlyWindowListener extends ApplicationAdapter
{
	
	private Event<Object> event_init = null;
	public Event<FlyRenderer2D> event_render = null;
	
	private FlyRenderer2D renderer;
	
	public FlyWindowListener()
	{
		this(null);
	}
	
	
	public FlyWindowListener(Event<Object> event_init)
	{
		this.event_init = event_init;
	}
	
	
	@Override
	public void create ()
	{
		if(this.event_init != null)
			this.event_init.invoke(null);
		this.event_init = null;
	}

	@Override
	public void resize (int width, int height)
	{
		
	}

	@Override
	public void render ()
	{
		Gdx.gl.glClearColor(0, 0, 0, 0);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
		if(event_render != null)
		{
			renderer.Begin(ShapeRenderer.ShapeType.Line);
			event_render.invoke(renderer);
			renderer.End();
		}
	}

	@Override
	public void pause ()
	{
		
	}

	@Override
	public void resume ()
	{
		
	}

	@Override
	public void dispose ()
	{
		event_render = null;
		renderer.Dispose();
	}
	
	
}














